package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.home.BR
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import kotlinx.coroutines.launch

class InvoiceVModel(application: Application) : BViewModel<BRepository>(application) {



    var isOneType = MutableLiveData<Boolean>(true)//默认个人
    var isTwoType = MutableLiveData<Boolean>(false)
    var invoceType: String? = "0"

    //方式

    var isOneWay = MutableLiveData<Boolean>(true)//默认个人
    var isTwoWay = MutableLiveData<Boolean>(false)
    var wayType: String? = "0"

    //点单
    var tips: MutableLiveData<String> = MutableLiveData()
    var order_ids:String?=null


    @Bindable
    var title: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.title)
        }

    @Bindable
    var duty: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.duty)
        }

    @Bindable
    var amount: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.amount)
        }

    @Bindable
    var email: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.email)
        }

    fun toChoseType(type: Int) {
        if (type == 1) {
            invoceType = "0"
            isOneType.value = true
            isTwoType.value = false
        } else {
            invoceType = "1"
            isOneType.value = false
            isTwoType.value = true
        }

    }

    fun toChoseWay(type: Int) {
        if (type == 1) {
            wayType = "0"
            isOneWay.value = true
            isTwoWay.value = false
        } else {
            wayType = "1"
            isOneWay.value = false
            isTwoWay.value = true
        }

    }

    fun toSubmit(){
        if(TextUtils.isEmpty(title)){
            toast("请输入企业名称")
            return
        }
        if(TextUtils.isEmpty(duty)){
            toast("请输入税号")
            return
        }
        if(TextUtils.isEmpty(order_ids)){
            toast("请选择相应的订单")
            return
        }
        if(TextUtils.isEmpty(amount)){
            toast("请输入开票金额")
            return
        }
        if(TextUtils.isEmpty(email)){
            toast("请输入收取电子发票的邮箱地址")
            return
        }

        viewModelScope.launch {
           var it= repository().lreq { NetEngine.service.invoiceAdd(title,duty,invoceType,order_ids,amount,wayType,email) }
            if(it.ok(true)){
                var con = repository().context as Activity
                con.finish()
            }
        }


    }


}



